package main.chapter4

import java.io.BufferedReader
import java.io.FileReader

fun main() {

    //结合data class
    findPerson()?.let { (name, age) ->
        println("$name,$age")
    }

    findPerson()?.let { person ->
        person.work()
        println(person.age)
    }

    //apply相当于在person作用域可以直接调用成员
    findPerson()?.apply {
        work()
        println(age)
    }

    val bufferReader = BufferedReader(FileReader("src/main/chapter4/hello.txt"))

    //相当于bufferReader的作用域，可以调用成员
    with(bufferReader) {
        var line: String?
        while (true) {
            line = readLine() ?: break
            println(line)
        }
        close()
    }

    BufferedReader(FileReader("src/main/chapter4/hello.txt")).use {
        var line: String?
        while (true) {
            line = it.readLine() ?: break
            println(line)
        }
    }

}

data class Person(val name: String, val age: Int) {
    fun work() {
        println("$name is working")
    }
}

fun findPerson(): Person? {
    return Person("studentName", 22)
}